<?php 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use App\Models\Device;
use App\Models\ProjectUser;
use App\Models\SwitchData;
use Hash;
use Log;
//20210322
class HongfaController extends Controller
{
    public function getHongfa(Request $request){
        $param = $request->all();
        $projectlist = ProjectUser::where(['user_id'=>$param['adminId']])->pluck('projectId')->toArray();
        $p = Device::where(['device_type'=>10])->where('pid','=',0)->whereIn('projectId',$projectlist)->get()->toArray();
        $list = Device::where(['device_type'=>10])->where('pid','!=',0)->whereIn('projectId',$projectlist)->get()->toArray();
        foreach ($p as $val){
            foreach ($list as &$dval){
                if($val['deviceId']==$dval['pid']){
                    $dval['pname'] = $val['devicename'];
                }
            }
        }
        return json_encode($list);
    }

    public function getHongfaData(Request $request){
        $param = $request->all();
        // $device = Device::find($param['deviceId']);
        $redisData = SwitchData::where(['device_id'=>$param['deviceId']])->limit(10)->orderBy('created_time', 'desc')->get()->toArray();
        if(isset($redisData)){
            $data['temp'] = [];
            $data['power'] = [];
            $data['current'] = [];
            $data['voltage'] = [];
            $data['time'] = [];
            $status = json_decode($redisData[0]['data'],1);
            $data['status'] = $status['status'];
            $param = array_reverse($redisData);
            foreach($param as $key => $val){
                $info = json_decode($val['data'], true);
                $data['temp'][] = $info['temp'];
                $data['power'][] = $info['power'];
                $data['current'][] = $info['current'];
                $data['voltage'][] = $info['voltage'];
                $data['time'][] = date('H:i:s',$val['created_time']);
            }
        }
        // print_r($data);die;
        return response()->json(['code'=>1, 'data'=> $data]);
    }
    
}